using AutoMapper;
using B.S.Medical.Api.Write.Application.Command.Doctor;
using B.S.Medical.Domain.Doctor;
using B.S.Medical.ErrorCode;
using B.S.Medical.Infrastructure.Interface;
using MediatR;
using Microsoft.Extensions.Logging;

namespace B.S.Medical.Api.Write.Application.Handler.Doctor
{
    /// <summary>
    /// 更新医生状态处理器
    /// </summary>
    public class UpdateDoctorStatusHandler : IRequestHandler<UpdateDoctorStatusCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<DoctorModel> _doctorRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<UpdateDoctorStatusHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        public UpdateDoctorStatusHandler(IBaseRepository<DoctorModel> doctorRepository,
                                      IMapper mapper,
                                      ILogger<UpdateDoctorStatusHandler> logger)
        {
            _doctorRepository = doctorRepository;
            _mapper = mapper;
            _logger = logger;
        }

        /// <summary>
        /// 处理更新医生状态命令
        /// </summary>
        /// <param name="request">更新医生状态命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>返回结果</returns>
        public Task<ApiResult<int>> Handle(UpdateDoctorStatusCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            try
            {
                // 查找医生
                var doctor = _doctorRepository.GetAll().Where(d => d.Id == request.DoctorId && !d.IsDeleted).FirstOrDefault();
                if (doctor == null)
                {
                    res.Msg = "未找到该医生!";
                    res.Code = ApiEnum.Fail;
                    _logger.LogError($"未找到医生，ID:{request.DoctorId}");
                    return Task.FromResult( res );
                }

                // 更新状态
                if (doctor.Status == true)
                {
                    doctor.Status = false;
                }
                if (doctor.Status == false)
                {
                    doctor.Status = true;
                }
                var result = _doctorRepository.UpdateAsync(doctor).Result;

                if (result>0)
                {
                    res.Msg = "更新医生状态成功!";
                    res.Code = ApiEnum.Success;
                    res.Data = result;
                    _logger.LogInformation($"更新医生状态成功，ID:{request.DoctorId}");
                }
                else
                {
                    res.Msg = "更新医生状态失败!";
                    res.Code = ApiEnum.Fail;
                    res.Data = result;
                    _logger.LogError($"更新医生状态失败，ID:{request.DoctorId}");
                }

                return Task.FromResult(res);
            }
            catch (Exception ex)
            {
                res.Msg = "更新医生状态异常: " + ex.Message;
                res.Code = ApiEnum.Fail;
                _logger.LogError(ex, $"更新医生状态异常: {ex.Message}");
                return Task.FromResult(res);
            }
        }
    }
}